<template>
  <div class="box">
    <div class="box1">
      <div class="box-img">
        <img src="../assets/logo.png" alt="" />
      </div>

      <div class="box-box">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="证号" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password" >
            <el-input v-model="ruleForm.password" type="password"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm"
              >登录</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name:"login",
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入证号", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) {
          return this.$message("用户信息填写不正确"); 
        } else {
          //2.提交表单
          var result = await this.$axios({
            url: "/api/private/v1/login",
            method: "POST",
            data:this.ruleForm
          }).catch((err) => console.log(err));
          console.log(result, "login");
          if (result.data.meta.status == 200) {
            localStorage.setItem("token", result.data.data.token);
            this.$message(result.data.meta.msg);
            this.$router.push("about");
          } else {
            this.$message(result.data.meta.msg);
          }
        }
      });
    }, 
    // 重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100vw;
  height: 100vh;
  background: #2b4b6b;
  position: relative;
  .box1 {
    width: 600px;
    height: 350px;
    background-color: #ffffff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .box-img {
      width: 150px;
      height: 150px;
      background: #eeeeee;
      border: 10px solid #fff;
      border-radius: 50%;
      text-align: center;
      line-height: 250px;
      position: absolute;
      top: -90px;
      left: 219px;
      box-shadow: 0 0 10px 4px #e7e0e0;
      img {
        width: 150px;
        height: 161px;
        padding-top: 1px;
        border-radius: 50%;
      }
    }
  }
}
.demo-ruleForm {
  position: relative;
  top: 100px;
  left: -20px;
}
</style>